<?php

namespace app\common\model;

use app\common\model\order\Log;
use think\Model;

/**
 * Order
 */
class Order extends Model
{
    // 表名
    protected $name = 'order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    public $steps = [
        '1' => [
            ['id' => '4', 'label' => '做图'],
            ['id' => '5', 'label' => '菲林'],
            ['id' => '6', 'label' => '网版'],
            ['id' => '7', 'label' => '调色'],
            ['id' => '8', 'label' => '打板'],
            ['id' => '12', 'label' => '送版']
        ],
        '2' => [
            ['id' => '9', 'label' => '网版'],
            ['id' => '10', 'label' => '做货样板及颜色'],
            ['id' => '11', 'label' => '做货'],
            ['id' => '12', 'label' => '送货']
        ]
    ];

    protected $append = [
        'step_label',
        'logs'
    ];

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getFilesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getStepLabelAttr($value, $data): string
    {
        if($value == '999') return '生产完成';
        $label = "";
        foreach ($this->steps[$data['type']] as $v){
            if ($v['id'] == $data['step']) {
                $label = $v['label']; break;
            }
        }
        return $label;
    }

    public function setFilesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\User::class, 'user_id', 'id');
    }

    public function department(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Department::class, 'department_id', 'id');
    }

    public function device(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Device::class, 'device_id', 'id');
    }

    public function productCategory(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\product\Category::class, 'product_category_id', 'id');
    }

    public function logs(): \think\model\relation\hasMany
    {
        return $this->hasMany(Log::class, 'order_id', 'id');
    }

    public function searchStatusAttr($query, $value, $data)
    {
        switch (intval($value)){
            case 1: // 已下单
                $query->where('state','=', 0);
                break;
            case 2: // 已确认
                $query->where('state','=', 1);
                break;
            case 3: // 生产中
                $query->where('state','=', 2);
                $query->where('step','<', 999);
                break;
            case 4: // 已发货
                $query->where('state','=', 2);
                $query->where('step','=', 999);
                break;
            case 5: // 已完成
                $query->where('state','=', 3);
                break;
        }
    }

    public function searchStaffStepAttr($query, $value, $data)
    {
        $query->where('state','in', ['0', '1']);
        $query->where('step','in', $value);
    }

    public function searchKeyAttr($query, $value, $data){
        if(!empty($value)){
            $user_ids = User::where('nickname', 'like', '%'.$value.'%')->column('id');
            $user_ids = array_values($user_ids);
            $user_ids = implode(',', $user_ids);
            $sql = ' no like "%'.$value.'%"';
            if($user_ids) {
                $sql .= ' or user_id in ('.$user_ids.')';
            }
            $query->where($sql);
        }
    }

    public function searchUserIdAttr($query, $value, $data){
        if(!empty($value)){
            $query->where('user_id', '=', $value);
        }
    }

    public function searchUserAttr($query, $value, $data){
        if(!empty($value)){
            $query->hasWhere('user', function($query) use ($value){
                $query->where('nickname', 'like', '%'.$value.'%');
            });
        }
    }

    public function searchDateAttr($query, $value, $data)
    {
        if(!empty($value)){
            $value = explode(' - ', $value);
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }

    public function searchNoAttr($query, $value, $data)
    {
        if(!empty($value)){
            $query->where('no', 'like', '%'.$value.'%');
        }
    }

    public function searchCateIdAttr($query, $value, $data)
    {
        if(!empty($value)){
            $query->where('product_category_id', '=', $value);
        }
    }

}